package com.igroupes.ovineherd.model.Style;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.enums.FormKey;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class NumberStyle extends InputStyle {
    private Object min = 0;
    private Object max = 999999999;
    private Integer precision;
    public NumberStyle(){

    }
    public NumberStyle(String label, Object value, Boolean required) {
        super(label, value,required);
        this.precision = 0;
        this.type = FormKey.NUMBER.getCode();
    }
    public NumberStyle(String label, Object value, Boolean required, String desc) {
        this(label, value, required);
        this.desc = desc;
    }
    public NumberStyle(String label, Object value, Boolean required, Object min) {
        this(label, value, required);
        this.min = min;
        this.value  = value;
    }
    public NumberStyle(String label, Object value, Object min, Integer precision, Boolean required) {
        this(label, value, required, min);
        this.precision = precision;
    }
    public NumberStyle(String label, Object value, Boolean required, Object min, Object max) {
        this(label, value, required, min);
        this.max = max;
    }
    public NumberStyle(String label, Object value, Boolean required,String desc, Object min, Object max) {
        this(label, value, required, min,max);
        this.desc = desc;
    }
    public NumberStyle(String label, Object value, Object min, Object max, Integer precision, Boolean required) {
        this(label, value, required, min, max);
        this.precision = precision;
    }

    @Override
    public Boolean verify(Object o) {
        if(!(o instanceof NumberStyle)){
            throw new ModelException("校验style类类型错误，number");
        }
        NumberStyle style = (NumberStyle) o;
        if(!style.getDisabled()){

            super.verify(o);
            if(!(style.getMax() instanceof Integer) || !(style.getMin() instanceof Integer)){
                throw new ModelException("必须是数字");
            }
            if(style.getRequired() ){
                if(style.getValue() == null || style.getValue() == ""){
                    throw new ModelException(style.getLabel()+"值必传");
                }
            }
            if( null != value){
                if(!(style.getValue() instanceof Integer)){
                    throw new ModelException(style.getLabel()+"值必须是数字");
                }
                Integer value = Convert.toInt(style.getValue());
                if(ObjectUtil.isNotNull(style.getMin()) && Convert.toInt(style.getMin()).compareTo(value) > 0){
                    throw new ModelException("不能小于"+style.getMin());
                }
                if(ObjectUtil.isNotNull(style.getMax()) && Convert.toInt(style.getMax()).compareTo(value) < 0){
                    throw new ModelException("不能大于"+style.getMax());
                }
            }
        }
        return true;
    }
}
